package com.dayaokeji.rhythmschoolstudent.client.common.exam


import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

import com.dayaokeji.rhythmschoolstudent.R
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseFragment
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.TestedExamListAdapter
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.UnTestedExamListAdapter
import com.dayaokeji.rhythmschoolstudent.wiget.WrapLinearLayoutManager
import com.dayaokeji.server_api.domain.Exam
import kotlinx.android.synthetic.main.fragment_un_tested.*
import java.io.Serializable


/**
 *  未测验 & 测验
 */
class TestFragment : BaseFragment() {

    companion object {
        private const val EXAM_LIST = "exam_list"

        private const val TYPE = "exam_type"
        val TYPE_UNTESTED = 1
        val TYPE_TESTED = 2

        fun newInstance(examList: ArrayList<Exam>,type: Int): TestFragment {
            val unTestedFragment = TestFragment()
            val bundle = Bundle()
            bundle.putSerializable(EXAM_LIST,examList as Serializable)
            bundle.putInt(TYPE,type)
            unTestedFragment.arguments = bundle
            return unTestedFragment
        }
    }

    private val unTestedExamListAdapter by lazy { UnTestedExamListAdapter() }
    private val testedExamListAdapter by lazy { TestedExamListAdapter() }

    private var examType: Int? = null
    private lateinit var examList: ArrayList<Exam>

    override fun onCreate(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View {
        return  inflater.inflate(R.layout.fragment_un_tested, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        init()
    }

    private fun init() {
        examType = arguments?.getInt(TYPE)
        examList = arguments?.getSerializable(EXAM_LIST) as ArrayList<Exam>

        setUpView()
    }

    private fun setUpView() {
        setRecyclerView()
        setOnListener()
    }

    private fun setRecyclerView() {
        rvExamList.layoutManager = WrapLinearLayoutManager(activity)
        unTestedExamListAdapter.setEmptyView(R.layout.empty_layout, rvExamList)
        testedExamListAdapter.setEmptyView(R.layout.empty_layout,rvExamList)

        examType?.let {
            if (it == TYPE_UNTESTED) {
                unTestedExamListAdapter.setNewData(examList)
                rvExamList.adapter = unTestedExamListAdapter
            }else if (it == TYPE_TESTED) {
                testedExamListAdapter.setNewData(examList)
                rvExamList.adapter = testedExamListAdapter
            }
        }
    }

    private fun setOnListener() {
        unTestedExamListAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { baseQuickAdapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, i: Int ->
            val exam = baseQuickAdapter.data[i] as Exam
            val intent = Intent(activity, AnswerActivity::class.java)
            intent.putExtra(AnswerActivity.EXTRA_TEST_NAME, exam.name)
            intent.putExtra(AnswerActivity.EXTRA_EXAM_ID, exam.id)
            intent.putExtra(AnswerActivity.EXTRA_EXAM_ENTITY, exam)
            intent.putExtra(AnswerActivity.EXTRA_SHOW_EXAM_RESULT, false)
            intent.putExtra(AnswerActivity.EXTRA_IS_QUESTION_AND_ANSWER,false)
            startActivity(intent)
        }
        testedExamListAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { baseQuickAdapter: BaseQuickAdapter<Any, BaseViewHolder>, view: View, position: Int ->
            val exam = baseQuickAdapter.data[position] as Exam
            val intent = Intent(activity, AnswerActivity::class.java)
            intent.putExtra(AnswerActivity.EXTRA_TEST_NAME, exam.name + " - 成绩")
            intent.putExtra(AnswerActivity.EXTRA_EXAM_ENTITY, exam)
            intent.putExtra(AnswerActivity.EXTRA_EXAM_ID, exam.id)
            intent.putExtra(AnswerActivity.EXTRA_SHOW_EXAM_RESULT, true)
            intent.putExtra(AnswerActivity.EXTRA_IS_QUESTION_AND_ANSWER, false)
            startActivity(intent)
        }
    }
}
